package com.website.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.website.main.service.S_User;
import com.website.mybatis.dao.UserDao;
import com.website.mybatis.entity.SysUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class S_UserImpl implements S_User{

	@Autowired
	UserDao dao;

	private String defaultPwd = "@123456";

	@Override
	public Map<String,Object> getUserList(String name,int page,int limit) {
		Map<String,Object> map = new HashMap<>();
		QueryWrapper<SysUser> w = Wrappers.query(new SysUser(name))
				.select("id", "name", "create_time", "update_time", "update_time", "role_id");
		map.put("data", dao.selectPage(new Page<>(page, limit), w).getRecords());
		map.put("count", dao.selectCount(Wrappers.emptyWrapper()));
		return map;
	}
	@Override
	public Map<String,Object> saveUser(SysUser user) {
		Map<String,Object> map = new HashMap<>();
		if(dao.selectCount(Wrappers.query(new SysUser(user.getName()))) > 0){
			map.put("success", false);
			map.put("msg", "用户名重复");
			return map;
		}
		user.setPassword(DigestUtils.md5Hex(user.getPassword()));
		user.setCreateTime(new Date());
		user.setAdmin(false);
		int count;
		try {
			count = dao.insert(user);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", e.getMessage());
			return map;
		}
		if(count > 0){
			map.put("success", true);
			map.put("msg", "新增用户成功");
			return map;
		}
		map.put("success", false);
		map.put("msg", "操作失败");
		return map;
	}
	@Override
	public Map<String, Object> updatePwd(String password) {
		Map<String,Object> map = new HashMap<>();
		SysUser t = (SysUser) SecurityUtils.getSubject().getPrincipal();
		SysUser user = dao.selectById(t.getId());
		if(user == null){
			map.put("success", false);
			map.put("msg", "用户不存在");
			return map;
		}
		user.setPassword(DigestUtils.md5Hex(password));
		if(dao.updateById(user) > 0){
			map.put("success", true);
			map.put("msg", "更新成功");
		}else{
			map.put("success", true);
			map.put("msg", "更新失败");
		}
		return map;
	}
	@Override
	public Map<String, Object> delUser(Integer id) {
		Map<String,Object> map = new HashMap<>();
		SysUser user = dao.selectById(id);
		if(user == null){
			map.put("success", false);
			map.put("msg", "相应用户不存在");
			return map;
		}
		SysUser t = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if(t.getId().equals(user.getId())){
			map.put("success", false);
			map.put("msg", "你不能删除自己");
			return map;
		}
		if(!t.getAdmin() && user.getAdmin()){
			map.put("success", false);
			map.put("msg", "你在尝试删除超级管理员，该操作已被禁止！");
			return map;
		}
		if(dao.deleteById(id) > 0){
			map.put("success", true);
			map.put("msg", "操作成功");
			return map;
		}
		map.put("success", false);
		map.put("msg", "操作失败");
		return map;
	}
	@Override
	public Map<String, Object> updUser(SysUser userNew) {
		Map<String,Object> map = new HashMap<>();
		SysUser user = dao.selectById(userNew.getId());
		if(user == null){
			map.put("success", false);
			map.put("msg", "相应用户不存在");
			return map;
		}
		SysUser t = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if(!t.getAdmin() && user.getAdmin()){
			map.put("success", false);
			map.put("msg", "你不能更改超级管理员的信息");
			return map;
		}
		if(t.getName().equals(user.getName())){
			map.put("success", false);
			map.put("msg", "你更新了自己的信息，重新登录生效");
			return map;
		}
		userNew.setAdmin(user.getAdmin());
		userNew.setPassword(DigestUtils.md5Hex(userNew.getPassword()));
		if(dao.updateById(userNew) > 0){
			map.put("success", true);
			map.put("msg", "操作成功");
			return map;
		}
		map.put("success", false);
		map.put("msg", "操作失败");
		return map;
	}
	@Override
	public Map<String, Object> resetPwd(Integer id) {
		Map<String,Object> map = new HashMap<>();
		SysUser user = dao.selectById(id);
		if(user == null){
			map.put("success", false);
			map.put("msg", "用户不存在");
			return map;
		}
		SysUser t = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if(!t.getAdmin() && user.getAdmin()){
			map.put("success", false);
			map.put("msg", "你正在尝试重置超级管理员登陆密码，该操作已被禁止！");
			return map;
		}
		user.setPassword(DigestUtils.md5Hex(user.getName() + defaultPwd));
		if(dao.updateById(user) > 0){
			map.put("success", true);
			map.put("msg", "更新成功");
		}else{
			map.put("success", true);
			map.put("msg", "更新失败");
		}
		return map;
	}
}